use tokio::task;
use reqwest::Client;
use std::sync::{Arc, atomic::{AtomicUsize, Ordering}};
use flume::{Sender, Receiver, bounded};
use futures::stream::{FuturesUnordered, StreamExt};
use std::time::Instant;

// 监控函数，定期检查请求数量、成功数、成功率和每秒请求数量
pub async fn monitor_requests(
    total_counter: Arc<AtomicUsize>,
    success_counter: Arc<AtomicUsize>,
    rx: Receiver<reqwest::Request>
) {
    let mut last_total = 0;
    let start_time = Instant::now();
    loop {
        tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
        let total = total_counter.load(Ordering::SeqCst);
        let success = success_counter.load(Ordering::SeqCst);
        let elapsed_secs = start_time.elapsed().as_secs();
        let success_rate = if total > 0 {
            success as f64 / total as f64 * 100.0
        } else {
            0.0
        };
        let per_sec = total - last_total;
        last_total = total;
        let remaining = rx.len();
        println!(
            "Total requests: {}, Success: {}, Success rate: {:.2}%, Requests per second: {}, Remaining in channel: {}",
            total, success, success_rate, per_sec, remaining
        );
    }
}